package jt.com.jt809.handler;


import jt.com.jt809.constants.JT809DataTypeConstants;
import jt.com.jt809.handler.msg.UpConnectHandler;
import jt.com.jt809.handler.msg.UpExgMsgHandler;
import jt.com.jt809.handler.msg.UpLinkTestHandler;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 初始化业务数据类型标识对应的处理类
 *
 * @Author OuJiaJun
 * @Date 2024/4/24 10:44
 */
@Component
public class CommonHandlerFactory implements ApplicationContextAware {
    private static final Map<Integer, CommonHandler> handlers = new ConcurrentHashMap<>();
    private static ApplicationContext context;


    @PostConstruct
    public void serverHandlerFactory() {
        // 主链路登录请求消息
        handlers.put(JT809DataTypeConstants.UP_CONNECT_REQ,context.getBean(UpConnectHandler.class));
        // 主链路动态信息交换消息
        handlers.put(JT809DataTypeConstants.UP_EXG_MSG,context.getBean(UpExgMsgHandler.class));
        // 主链路连接保持请求消息
        handlers.put(JT809DataTypeConstants.UP_LINKTEST_REQ,context.getBean(UpLinkTestHandler.class));
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if(null == context){
            context = applicationContext;
        }
    }

    public CommonHandler getHandler(Integer flag){
        return handlers.get(flag);
    }

}
